﻿using APP.IO;
using APP.OpenCV;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MinesweeperAutoScanDemo;
using ScreenCapture;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace UnitTestForSln
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestMethod1()
        {

        }

        private void Button_Click()
        {
            //var sc = new ScreenCapture.ScreenCaptureHelper();
            //sc.ScreenCapture_Click();

            //var state = this.WindowState;
            //this.WindowState = WindowState.Minimized;
            var cawindow = new CaptureWindow();
            var backImage = cawindow.ShowDialog();
            //this.WindowState = state;
            var imageRect = cawindow.selectImageRect;
            if (backImage != null)
            {
                //backImage = CaptureWindow.CapturePrimaryScreen();
                //var temPath = System.IO.Path.GetTempFileName();
                var imageFolder = PathHelper.GetPath(FolderType.Temp, "Image");
                var imageFile = PathHelper.GetTempFileName(imageFolder, ".png");
                backImage.Save(imageFile, System.Drawing.Imaging.ImageFormat.Png);
                //var imageSource = ImageHelper.ChangeBitmapToImageSource(backImage);
                //var imageBitmap = ImageHelper.ImageSourceToBitmap(imageSource);

                var imageChips = OCRHelper.SplitImage(backImage, 30, 24);

                List<Task> imageTasks = new List<Task>();
                foreach (var item in imageChips)
                {
                    var itemTask = Task.Run(() =>
                    {
                        try
                        {
                            var ocrHelperItem = new OpenCVOCRHelper();

                            var ocrBack = ocrHelperItem.OCRText(item.DeBUGPath);
                            if (ocrBack.Success)
                            {
                                item.OCRText = ocrBack.Data.ToString();
                            }
                        }
                        catch (Exception ex)
                        {

                        }
                    });
                    imageTasks.Add(itemTask);
                }
                Task.WaitAll(imageTasks.ToArray());
                var hasNumberItem = imageChips.Where(x => !string.IsNullOrEmpty(x.OCRText)).ToList();

            }
        }

        private void Button2_Click()
        {

            string pppatr = @"C:\Users\Administrator\Documents\MyAPP\Temp\Image\acbb59.png";
            //OpenCVHelper.TestBoard(pppatr);
            //return;

            var backImage = System.Drawing.Bitmap.FromFile(pppatr);

            var imageChips = OCRHelper.SplitImage(backImage, 30, 24);

            List<Task> imageTasks = new List<Task>();
            foreach (var item in imageChips)
            {
                var itemTask = Task.Run(() =>
                {
                    try
                    {
                        var ocrHelperItem = new OpenCVOCRHelper();

                        var ocrBack = ocrHelperItem.OCRText(item.DeBUGPath);
                        if (ocrBack.Success)
                        {
                            item.OCRText = ocrBack.Data.ToString();
                        }
                    }
                    catch (Exception ex)
                    {

                    }
                });
                imageTasks.Add(itemTask);
            }
            Task.WaitAll(imageTasks.ToArray());
            var hasNumberItem = imageChips.Where(x => !string.IsNullOrEmpty(x.OCRText)).ToList();

        }


        public void TestSplitImage()
        {
            string pppatr = @"C:\Users\Administrator\Documents\MyAPP\Temp\Image\070831396e494715a2e578af25b909a3.png";
            //OpenCVHelper.TestBoard(pppatr);
            //return;

            var backImage = System.Drawing.Bitmap.FromFile(pppatr);

            var imageChips = OCRHelper.SplitImage(backImage, 30, 24);


            return;
        }
        public void TestDrawLine()
        {
            string pppatr = @"C:\Users\Administrator\Documents\MyAPP\Temp\Image\070831396e494715a2e578af25b909a3.png";
            //OpenCVHelper.TestBoard(pppatr);
            //return;

            var backImage = System.Drawing.Bitmap.FromFile(pppatr);

            //var imageChips = OCRHelper.SplitImage(backImage, 30, 24);
            var imageChips = OCRHelper.SplitImageTestDrawLine(backImage, 30, 24);

            return;
        }

        public void TestBoard()
        {
            string pppatr = @"C:\Users\Administrator\Documents\MyAPP\Temp\Image\070831396e494715a2e578af25b909a3.png";
            OpenCVHelper.TestBoard(pppatr);
            //return;
        }

        private void Button3_Click()
        {
            var ocrHelperItem = new ImageMatchModel();
            var sourceFolder = @"C:\Users\Administrator\Documents\MyAPP\Temp\OrigineTemplate\";
            ocrHelperItem.InitSource(sourceFolder);

            var strFolder = @"C:\Users\Administrator\Documents\MyAPP\Temp\ImageTest2\";
            var files = new DirectoryInfo(strFolder).GetFiles();
            List<object> list = new List<object>();
            foreach (var file in files)
            {
                var data = ocrHelperItem.MatchTemplate(file.FullName, out double meanH);
                if (data != null)
                {
                    list.Add(data);
                }
            }
        }
    }
}
